﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication3
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillData();
            }
        }
        //取消
        protected void gv3_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gv3.EditIndex = -1;
            FillData();
        }
        //删
        protected void gv3_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var id = GetCellValue(e.RowIndex, 0);
            var sql = string.Format("delete Articles where id ='{0}'", id);
            DbHelper.AddOrDeleteOrUpdate(sql);
            gv3.EditIndex = -1;
            FillData();
        }

        protected void gv3_RowEditing(object sender, GridViewEditEventArgs e)
        {
           
            gv3.EditIndex = e.NewEditIndex;
            FillData();
        }
        //更 插
        protected void gv3_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var id = GetCellValue(e.RowIndex, 0);
            var Title = GetCellValue(e.RowIndex, 1);
            var Content = GetCellValue(e.RowIndex,2);
            var Author = GetCellValue(e.RowIndex, 3);
            var Remarks = GetCellValue(e.RowIndex, 4);
            var isOK = int.TryParse(id, out int Id);
            if (isOK)
            {
                //id 存在  更新
                var sql = string.Format("update Articles set title='{0}',content='{1}',author='{2}',remarks='{3}'where id='{4}'"
               , Title, Content, Author, Remarks, id);
                
                DbHelper.AddOrDeleteOrUpdate(sql);
            }
            else
            {
                // 添加
                var sql = string.Format("insert into Articles(title,content,author,remarks) values ('{0}','{1}','{2}','{3}') "
                    ,Title,Content,Author,Remarks);

                DbHelper.AddOrDeleteOrUpdate(sql);

            }
            gv3.EditIndex = -1;
            FillData();
        }
        //添加按钮
        protected void Add_Click(object sender, EventArgs e)
        {
            var sql = "select * from Articles";
            var tempDt = DbHelper.GetData(sql);
            var dt = tempDt.Copy();

            dt.Rows.Add(dt.NewRow());

            gv3.DataSource = dt;
            gv3.DataBind();

            gv3.EditIndex = dt.Rows.Count - 1;
            gv3.DataSource = dt;
            gv3.DataBind();

        }
        
        public void FillData()
        {
            var sql = "select id,Title,Content,Author,Remarks from Articles;";
            var dt = DbHelper.GetData(sql);

            gv3.DataSource = dt;
            gv3.DataBind();
        }
        private string GetCellValue(int rowIndex,int colIndex)
        {
            var control = gv3.Rows[rowIndex].Cells[colIndex];
            var res = control.Controls.Count > 0 ? ((TextBox)control.Controls[0]).Text : control.Text;
            return res;
        }

       
        }

    }
